﻿using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PMS
{
    public static class DD
    {
        public static void SendDingDingMessage(BD_ReviewTotal revtotal)
        {
            string curr = revtotal.CurrentReviewer;
            string[] curs = curr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for(int i=0;i<curs.Length;i++)
            {
            //    int userid = int.Parse(curs[i]);
            //    BD_UserInfo user = DALSession.Current.BD_UserInfoDAL.GetListBy(u=>u.ID==userid).FirstOrDefault();
            //    string accessToken = GetToken();
            //    //发送消息
            //    IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            //    OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
            //    req.AgentId = 300873190;
            //    req.UseridList = user.DDUserid;
            //    req.ToAllUser = false;
            //    DDMessage message = new DDMessage();
            //    message.msgtype = "link";
            //    message.link = new DDLink() { 
            //        messageUrl="http://wuruiwhut.vaiwan.com",
            //        picUrl="nopicture",
            //        title = "您有新的审批任务！",
            //        text =  "您当前有编号：" +revtotal.RevNum+"的"+revtotal.DocName+"任务,请您尽快审批！"
            //    };
            //    req.Msg = JsonConvert.SerializeObject(message);
            //    OapiMessageCorpconversationAsyncsendV2Response rsp = client.Execute(req, accessToken);
            //    string err = rsp.ErrMsg;
            }
        }
        public static string GetToken()
        {
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.Appkey = "ding7uvm3uruwutcavag";//Appkey
            request.Appsecret = "tGB46wmWSHHSTL9W2e6201xhvah8Pr0OBgGlBBG67eO8kAOggMRqOzjXFDhiLG_Y";//Appsecret
            request.SetHttpMethod("GET");
            OapiGettokenResponse response = client.Execute(request);
            return response.AccessToken;
        }
    }
}